package com.yiwei.zxp.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.ChangePasswordBean;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.util.MD5;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.V19FrameLayout;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class ChangePasswordActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_pass_now)
    EditText editPassNow;
    @BindView(R.id.btn_pass_now_clear)
    ImageView btnPassNowClear;
    @BindView(R.id.btn_pass_now_visible)
    ImageView btnPassNowVisible;
    @BindView(R.id.edit_pass_new)
    EditText editPassNew;
    @BindView(R.id.btn_pass_clear)
    ImageView btnPassClear;
    @BindView(R.id.btn_pass_visible)
    ImageView btnPassVisible;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.txt_pass_tips)
    TextView txtPassTips;

    private Unbinder unbiner;
    private boolean isNowVisible;
    private boolean isVisible;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("修改密码");
        txtRight.setVisibility(View.VISIBLE);
        txtRight.setText("客服");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    public void getChangePassword(String old_password, String new_password) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        ChangePasswordBean bean = new ChangePasswordBean();
        bean.old_password = MD5.getMD5str(old_password);
        bean.new_password = MD5.getMD5str(new_password);
        Api.api_service.getChangePassword(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "修改成功");
                    finish();
                });
    }


    private boolean passMatch(String passwd) {
        Pattern p = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$");

        Matcher m = p.matcher(passwd);
        return m.matches();
    }

    @OnTextChanged(R.id.edit_pass_new)
    void afterTextChanged2(Editable s) {
        if (TextUtils.isEmpty(UI.toString(editPassNew)) || TextUtils.isEmpty(UI.toString(editPassNow))) {
            btnSubmit.setSelected(false);
            btnPassClear.setVisibility(View.GONE);
        } else {
            btnSubmit.setSelected(false);
            btnPassClear.setVisibility(View.VISIBLE);
            btnSubmit.setEnabled(false);
            if (UI.toString(editPassNew).length() >= 6) {
                if (!passMatch(UI.toString(editPassNew))) {
                    txtPassTips.setText("密码必须是由6-20位字母和数字组成");
                    txtPassTips.setTextColor(0xffFF0030);
                } else {
                    btnSubmit.setEnabled(true);
                    txtPassTips.setText("密码由6-20位字母和数字组成，字母需区分大小写");
                    txtPassTips.setTextColor(0xffAAAAAA);
                    btnSubmit.setSelected(true);
                }
            }
        }
    }


    @OnClick({R.id.btn_back, R.id.txt_right, R.id.btn_pass_now_clear, R.id.btn_pass_now_visible, R.id.btn_pass_clear, R.id.btn_pass_visible, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                Util.startActivity(this, WechatChatActivity.class);
                break;
            case R.id.btn_pass_now_clear:
                editPassNow.setText("");
                break;
            case R.id.btn_pass_now_visible:
                if (!isNowVisible) {
                    //如果选中，显示密码
                    editPassNow.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    btnPassNowVisible.setImageResource(R.mipmap.img_eyes_open);
                } else {
                    //否则隐藏密码
                    editPassNow.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    btnPassNowVisible.setImageResource(R.mipmap.img_eyes_close);
                }
                isNowVisible = !isNowVisible;
                break;
            case R.id.btn_pass_clear:
                editPassNew.setText("");
                break;
            case R.id.btn_pass_visible:
                if (!isVisible) {
                    //如果选中，显示密码
                    editPassNew.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    btnPassVisible.setImageResource(R.mipmap.img_eyes_open);
                } else {
                    //否则隐藏密码
                    editPassNew.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    btnPassVisible.setImageResource(R.mipmap.img_eyes_close);
                }
                isVisible = !isVisible;
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editPassNow))) {
                    ToastUtil.makeText(this, "原密码不能为空");
                    return;
                }
                if (UI.toString(editPassNew).length() < 6) {
                    ToastUtil.makeText(this, "新密码至少为6位");
                    return;
                }
                getChangePassword(UI.toString(editPassNow), UI.toString(editPassNew));
                break;
        }
    }
}
